package com.mlm.mlmapp.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.androidquery.AQuery;
import com.androidquery.callback.AjaxCallback;
import com.androidquery.callback.AjaxStatus;
import com.mlm.mlmapp.R;
import com.mlm.mlmapp.interfaces.Prefs_;
import com.mlm.mlmapp.utils.CommonUtils;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.sharedpreferences.Pref;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yd on 2016/3/7.
 */
@EActivity(R.layout.forgetpwd)
public class ForgetpwdActivity extends BaseActivity {
    AQuery aq;
    @Pref
    Prefs_ pref;
    @ViewById
    TextView tv_title;
    @ViewById
    EditText et_forget_phone;
    @ViewById
    EditText et_forgetpwd;
    @ViewById
    EditText et_forget_yzm;
    @ViewById
    EditText et__phone;
    @ViewById
    Button btn_sendyzm;
    AjaxCallback<JSONObject> forgetCB;
    AjaxCallback<JSONObject> sendYZMCB;
    private String tempMobile = "";

    @Click
    void btn_sendyzm() {
        sendYZMAction();
        // set to state to disable

    }
    @AfterViews
    public void init() {

        String title = "忘记密码";
        if (!"".equals(title)) {
            ((TextView) this.findViewById(R.id.tv_title)).setText(title);
        }
        aq = new AQuery(this);
        aq.hardwareAccelerated11();
        forgetCB = new AjaxCallback<JSONObject>() {

            @Override
            public void callback(String url, JSONObject html, AjaxStatus status) {
                if (status.getCode() == -101) {
                    Toast.makeText(ForgetpwdActivity.this, "网络异常,请检查网络",
                            Toast.LENGTH_SHORT);
                    return;
                }
                if (status.getCode() != 200) {
                    Toast.makeText(aq.getContext(), "网络异常:" + status.getCode(),
                            Toast.LENGTH_LONG).show();
                } else {
                    try {
                        if (html.getBoolean("isSuccess")) {
                            pref.edit().loginID().put(tempMobile).apply();
                            Toast.makeText(aq.getContext(), "密码成功",
                                    Toast.LENGTH_LONG).show();
                            onSuccess();

                        } else {
                            Toast.makeText(aq.getContext(),
                                    html.getString("errors"), Toast.LENGTH_LONG)
                                    .show();
                        }
                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                }
            }
        };
        sendYZMCB = new AjaxCallback<JSONObject>() {

            @Override
            public void callback(String url, JSONObject html, AjaxStatus status) {
                if (status.getCode() == -101) {
                    Toast.makeText(ForgetpwdActivity.this, "网络异常,请检查网络",
                            Toast.LENGTH_SHORT);
                    return;
                }
                if (status.getCode() != 200) {
                    Toast.makeText(aq.getContext(), "网络异常:" + status.getCode(),
                            Toast.LENGTH_LONG).show();
                } else {
                    try {
                        if (html.getBoolean("isSuccess")) {

                            Toast.makeText(aq.getContext(), "发送成功",
                                    Toast.LENGTH_LONG).show();
                            // MainActivity_.intent(LoginActivity.this).start();
                            btn_sendyzm.setClickable(false);
                            btn_sendyzm
                                    .setBackgroundResource(R.drawable.grey_edge_btn);
                            btn_sendyzm.setTextColor(Color
                                    .parseColor("#d0d0d0"));
                            CountDownTimer timer = new CountDownTimer(period,
                                    step) {

                                @Override
                                public void onTick(long arg0) {
                                    // TODO Auto-generated method stub

                                    // tick tick tick...

                                    btn_sendyzm.setText(getString(
                                            R.string.hint_reg_waityzm,
                                            arg0 / 1000));

                                }

                                @Override
                                public void onFinish() {
                                    // TODO Auto-generated method stub
                                    // get back to normal
                                    btn_sendyzm.setClickable(true);
                                    btn_sendyzm.setTextColor(Color
                                            .parseColor("#ea2626"));
                                    btn_sendyzm
                                            .setText(R.string.hint_reg_sendyzm);
                                    btn_sendyzm
                                            .setBackgroundResource(R.drawable.red_edge_btn);

                                }
                            };
                            timer.start();
                        } else {
                            Toast.makeText(aq.getContext(),
                                    html.getString("errors"), Toast.LENGTH_LONG)
                                    .show();
                        }
                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                }
            }
        };

    }
    private void sendYZMAction() {
        String mobile = et_forget_phone.getText().toString().trim();
        List<NameValuePair> pairs = new ArrayList<NameValuePair>();
        if (CommonUtils.isMobileNO(mobile)){
            pairs.add(new BasicNameValuePair("mobile", mobile));
            pairs.add(new BasicNameValuePair("type", AUTH_CODE_FORGET));
            CommonUtils.postRequest(sendyzm_action, aq, sendYZMCB, pairs);
        }else{
            Toast.makeText(ForgetpwdActivity.this,"手机号格式不正确",Toast.LENGTH_SHORT).show();
        }

    }
    public boolean forgetValidateEmpty() {
        List<View> validateList = new ArrayList<View>();
        et_forget_phone.setTag("phone");
        validateList.add(et_forget_phone);
        validateList.add(et_forgetpwd);
        validateList.add(et_forget_yzm);
        return CommonUtils.validateETEmpty(this, validateList);
    }

    @Click
    void btn_forget_action() {

        if (forgetValidateEmpty()) {
            forgetAction();

        }

    }

    private void forgetAction() {
        String mobile = et_forget_phone.getText().toString().trim();
        String passwd = et_forget_yzm.getText().toString().trim();
        String authcode =et_forgetpwd .getText().toString().trim();
        List<NameValuePair> pairs = new ArrayList<NameValuePair>();
        if (CommonUtils.isMobileNO(mobile)){
            pairs.add(new BasicNameValuePair("mobile", mobile));
            if (CommonUtils.isCorrectUserPwd(passwd)){

                    pairs.add(new BasicNameValuePair("authcode", authcode));
                    pairs.add(new BasicNameValuePair("passwd", passwd));
                    CommonUtils.postRequest(forget_action, aq, forgetCB, pairs);
                    tempMobile = mobile;

            }else{
                Toast.makeText(ForgetpwdActivity.this,"密码必须为6-16位英文字母与数字的组合",Toast.LENGTH_SHORT).show();
            }
        }else{
            Toast.makeText(ForgetpwdActivity.this,"手机号格式不正确",Toast.LENGTH_SHORT).show();
        }
    }
    void onSuccess() {
////        index=getIntent().getIntExtra("index", -1);
//        Intent intent = this.getIntent();
//        intent.putExtra("flag", true);
//        intent.putExtra("collectionid", collectionid);
//        intent.putExtra("collectionstatus", collectionstatus);
//        intent.putExtra("collectionstatus", index);
//        if(index!=-1){
//            this.setResult(HairDresserDetailActivity.RESULT_STYLE_COLLECTION, intent);
//        }else{
//            this.setResult(HairDresserDetailActivity.RESULT_DRESSER_COLLECTION, intent);
//        }

        Intent intent = new Intent(this,UserInfoActivity_.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
        this.finish();

    }
    @Click
    void btn_back() {
        onBackPressed();
    }

    @Override
    public void onBackPressed() {
        // TODO Auto-generated method stub
        super.onBackPressed();
        Intent data = new Intent();
        setResult(RESULT_CANCELED, data);
        this.finish();
    }
}
